﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FTools.Dominio;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using FTools.Generico;

namespace FTools.Persistencia
{
    class PerPais
    {
        static string msg = "";

        //OBTENER PAIS
        public Pais obtenerPais(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdPais)
        {
            Pais objPais = new Pais();
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select * from PAIS where ID_PAIS=@IdPais");

                command.Parameters.AddWithValue("@IdPais", xIdPais);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];

                    objPais.IdPais = (long)fila.ItemArray[0];
                    objPais.Nombre = (String)fila.ItemArray[1]; objPais.Nombre = objPais.Nombre.Trim();
                }
                return objPais;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //EXISTE PAIS
        public Boolean existePais(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdPais)
        {
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select count(*) as EXISTE from PAIS where ID_Pais=@IdPais");

                command.Parameters.AddWithValue("@IdPais", xIdPais);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    if ((int)fila.ItemArray[0] > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //EXISTE PAIS
        public Boolean existePais(SqlConnection xConexion, SqlTransaction xTransaccion, String xNombre)
        {
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select count(*) as EXISTE from PAIS where NOMBRE=@Nombre");

                command.Parameters.AddWithValue("@Nombre", xNombre.Trim());
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    if ((int)fila.ItemArray[0] > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //OBTENER COLECCION PAIS
        public ArrayList obtenerColeccionPais(SqlConnection xConexion, SqlTransaction xTransaccion)
        {
            SqlDataAdapter dataAdapter;
            DataTable dataTable = new DataTable();
            ArrayList colPais = new ArrayList();

            try
            {
                SqlCommand command = new SqlCommand("select * from PAIS");

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    Pais objPais = new Pais();
                    DataRow fila = dataTable.Rows[i];
                    objPais.IdPais = (long)fila.ItemArray[0];
                    objPais.Nombre = (String)fila.ItemArray[1]; objPais.Nombre = objPais.Nombre.Trim();
                    colPais.Add(objPais);
                }
                return colPais;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //ALTA PAIS
        public int altaPais(SqlConnection xConexion, SqlTransaction xTransaccion, Pais xObjPais)
        {
            try
            {
                SqlCommand command = new SqlCommand("insert into PAIS values (@IdPais, @Nombre)");

                command.Parameters.AddWithValue("@IdPais", xObjPais.IdPais);
                command.Parameters.AddWithValue("@Nombre", xObjPais.Nombre.Trim());

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //BAJA PAIS
        public int bajaPais(SqlConnection xConexion, SqlTransaction xTransaccion, Pais xObjPais)
        {
            try
            {
                SqlCommand command = new SqlCommand("delete from PAIS where ID_PAIS=@IdPais");

                command.Parameters.AddWithValue("@IdPais", xObjPais.IdPais);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //MODIFICA PAIS
        public int modificaPais(SqlConnection xConexion, SqlTransaction xTransaccion, Pais xObjPais)
        {
            try
            {
                SqlCommand command = new SqlCommand("update PAIS set NOMBRE=@Nombre " +
                                                    "where ID_Pais = @IdPais");

                command.Parameters.AddWithValue("@IdPais", xObjPais.IdPais);
                command.Parameters.AddWithValue("@Nombre", xObjPais.Nombre.Trim());

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }
    }
}
